﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace Pavilion.DesignerTool
{
    /// <summary>
    /// Interaction logic for ToolsView.xaml
    /// </summary>
    public partial class ToolsView : UserControl, INotifyPropertyChanged
    {
        public static readonly DependencyProperty CurrentLayerProperty = DependencyProperty.Register("CurrentLayer", typeof(TextureData), typeof(ToolsView));
        public static readonly DependencyProperty CurrentFrameDataProperty = DependencyProperty.Register("CurrentFrameData", typeof(FrameData), typeof(ToolsView),
            new PropertyMetadata(new PropertyChangedCallback(CurrentFrameDataChanged)));

        public event PropertyChangedEventHandler PropertyChanged;

        public TextureData CurrentLayer { get { return (TextureData)GetValue(CurrentLayerProperty); } set { SetValue(CurrentLayerProperty, value); } }
        public FrameData CurrentFrameData { get { return (FrameData)GetValue(CurrentFrameDataProperty); } set { SetValue(CurrentFrameDataProperty, value); } }

        public IDrawTool CurrentDrawTool { get { return currentDrawTool; } set { currentDrawTool = value; OnPropertyChanged("CurrentDrawTool"); } }
        public int Scale { get { return scale; } set { scale = value; OnPropertyChanged("Scale"); } }

        private int scale;
        private IDrawTool currentDrawTool;
        public ToolsViewModel ViewModel { get; set; }

        public ToolsView()
        {
            InitializeComponent();
            
            DataContext = ViewModel = new ToolsViewModel();
            colorGrid.SelectedColorChanged += (Color selectedColor) => ViewModel.SetBrushColor(selectedColor);

            Scale = 100;
        }

        private bool _isLoaded;
        private void ToolsView_Loaded(object sender, RoutedEventArgs e)
        {
            if (_isLoaded)
                return;

            if (!DesignerProperties.GetIsInDesignMode(this))
                ViewModel.Load();

            _isLoaded = true;
        }

        private void ApplyButton_Click(object sender, RoutedEventArgs e)
        {
            ViewModel.ApplyCanvasBoundsOnTexture(CurrentLayer);
        }

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        private void drawToolListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            CurrentDrawTool = ((FrameworkElement)drawToolListBox.SelectedItem).DataContext as IDrawTool;
        }

        static private void CurrentFrameDataChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
        {
            ToolsView view = (ToolsView)dependencyObject;

            view.ViewModel.SetCurrentFrame(view.CurrentFrameData);
        }
    }
}
